public class theThreads implements Runnable {
	private long sleeptime = (long)( Math.random()*10);
	private String name;
	private BinarySemaphore f; 
	private BinarySemaphore g; 
	private Semaphore h; 

	public theThreads(String threadname,BinarySemaphore f,BinarySemaphore g,Semaphore h) {
		this.name = threadname;
		this.f=f;
		this.g=g;
		this.h=h;
		Thread t = new Thread(this,threadname);
		t.start();
	}

	public void run() {
		while (true) {
			if (name.equals("H")) {
				try {
					Thread.sleep(sleeptime);
					System.out.println("H");
					h.V();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			else if (name.equals("F")) {
				try {
				    h.P();
					f.P();
					Thread.sleep(sleeptime);
					System.out.println("F");
					g.V();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else if (name.equals("G")) {

				try {
					g.P();
					Thread.sleep(sleeptime);
					System.out.println("G");
					f.V();

				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}

	}
}